package com.guo.hui.controller;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.guo.hui.dao.blog.TpArticleMapper;
import com.guo.hui.model.blog.TpArticle;
import com.guo.hui.model.shiro.TpUser;
import com.guo.hui.service.BlogArticleService;
import com.guo.hui.util.UtilJson;

@Controller
@RequestMapping("blogarticle")
public class BlogArticleController extends BaseController {

	@Autowired
	private BlogArticleService articleService;
	@Autowired
	private TpArticleMapper articleDao;

	@RequestMapping("/article")
	public String article() {
		return "blog/article/article";
	}

	@RequestMapping("/articleadd")
	public String articleadd() {
		return "blog/article/articleadd";
	}

	@ResponseBody
	@RequestMapping("/articlesave")
	public String articlesave(@RequestParam String tp) {
		TpArticle article = (TpArticle) UtilJson.fromJson(tp, TpArticle.class);
		TpUser user = (TpUser) getUserInfo();
		article.setAuthor(user.getUsercode());
		article.setUsername(user.getUsername());
		articleService.saveArticle(article);
		return "ok";
	}

	@RequestMapping("/update")
	public String update(Model mm, @RequestParam int id) {
		TpArticle tpArticle = articleDao.selectById(id);
		mm.addAttribute("tp", tpArticle);
		return "blog/article/update";
	}

	@ResponseBody
	@RequestMapping("/updatedata")
	public String updatedata(@RequestParam String tp) {
		TpArticle article = (TpArticle) UtilJson.fromJson(tp, TpArticle.class);
		articleService.updateArticle(article);
		return "ok";
	}

	@ResponseBody
	@RequestMapping("/articledata")
	public String articledata(Model mm) {
		List<TpArticle> list = articleService.selectArticleByMap(new HashMap<String, Object>());
		return UtilJson.toJson(list);
	}

	@ResponseBody
	@RequestMapping("/delete")
	public String delete(@RequestParam int id) {
		articleService.deleteArticle(id);
		return "ok";
	}

}
